package com.zc.common.mq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * 1. 完成统一的幂等封装
 * <p>Description : </p>
 *
 * @author : xuhongcao
 * @version : v1.0.0
 * @since : 6/4/2022
 **/
@Slf4j
public abstract class DefaultConsumer implements RocketMQListener<MessageExt> {

    public abstract TransactionTemplate getTransactionTemplate();

    public abstract ReceiveMessageDao getReceiveMessageDao();

//    public abstract Class<T> getMessageType();


    @Override
    public final void onMessage(MessageExt message) {
        String messageKey = message.getProperty("KEYS");
        log.info("messageKey:{}", messageKey);
        boolean consumed = this.isConsumed(messageKey);
        if (consumed) {
            log.error("messageId:{},已消费,", messageKey);
        } else {

        }
    }

    /**
     * 判断是否重复
     *
     * @param messageKey
     * @return
     */
    private boolean isConsumed(String messageKey) {
        return this.getReceiveMessageDao().existByMessageKey(messageKey);
    }
}
